package com.example.firstdemo.controller;

import com.example.firstdemo.entity.User;
import com.example.firstdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/users")
    public List<User> listUser() {
        return userService.listUser();
    }

    @GetMapping("/listUserByName")
    public List<User> listUserByName(String name) {
        return userService.findByGoodsName(name);
    }


//    @DeleteMapping("/good")
    @RequestMapping(value = "/good",method = RequestMethod.GET)
    public String delete(int goodsId) {
        int result = userService.delete(goodsId);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    //@PatchMapping(value = "/update")
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public String update(@RequestBody  User user) {
        int result = userService.update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    //@PostMapping(value = "/insert")
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    public User insert(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /*@RequestMapping("/ListUser")
    public String ListUser(Model model){
        List<User> users = userService.ListUser();
        model.addAttribute("users",users);
        return "ListUser";
    }

    //@GetMapping( value = "/findUserByGoodsName")
    @RequestMapping(value = "/findUserByGoodsName")
    public List<User> findUserByGoodsName(String goodsName){
        return userService.findByGoodsName(goodsName);
    }

    //@DeleteMapping(value = "/delete")
    @RequestMapping(value = "/delete")
    public void delete(int goodsId){
        userService.delete(goodsId);
    }

    //@PatchMapping(value = "/update")
    @RequestMapping(value = "/update")
    public void Update(int goodsId,String goodsName,int goodsType){
        User user = new User();
        user.setGoodsId(goodsId);
        user.setGoodsName(goodsName);
        user.setGoodsType(goodsType);
        userService.Update(user);
    }

    @PostMapping(value = "/insert")
    public void insert(User user){
        userService.insertUser(user);
    }*/

}

